﻿using System.Linq;
using Extreme.SPFramework.Configuration;
using Extreme.SPFramework.DependencyInjection;
using Extreme.SPFramework.Test.Entities;
using NUnit.Framework;

namespace Extreme.SPFramework.Test.Configuration
{
    /// <summary>
    /// Repeatable tests
    /// </summary>
    [TestFixture]
    public class ConfigurationTestFixture
    {
        /// <summary>
        /// Tests configuration section
        /// </summary>
        [Test]
        public void TestConfigurationSection()
        {
            // Get configuration
            ConfigurationSection configSection = new AppConfig().GetConfiguration();

            // Test
            Assert.IsNotNull(configSection);
            Assert.AreEqual(configSection.FontStyle, "bg-color: red");
            Assert.AreEqual(configSection.StartHour, 9);
            Assert.IsNotNull(configSection.Mappings);
            Assert.IsNotEmpty(configSection.Mappings);
            Assert.AreEqual(configSection.Mappings[0].EntityType, "Extreme.SPFramework.Test.Entities.TestEntity, Extreme.SPFramework.Test");
            Assert.AreEqual(configSection.Mappings[0].ListName, "Testowa lista");

            var mappings = configSection.Mappings[0].PropertyMappings;
            Assert.IsNotNull(mappings);
            Assert.IsNotEmpty(mappings);
            Assert.AreEqual("Name", mappings[0].PropertyName);
            Assert.AreEqual("Nazwa", mappings[0].ColumnName);
            Assert.AreEqual(false, mappings[0].ReadOnly);
        }

        /// <summary>
        /// Tests dependency injection in configuration
        /// </summary>
        [Test]
        public void TestMappingConfigurationInjection()
        {
            var mappingConfigProvider = ServiceProvider.GetInstance<IMappingConfiguration>();
            Assert.IsNotNull(mappingConfigProvider);
            Assert.IsInstanceOf<AppConfig>(mappingConfigProvider);

            var allConfigProviders = ServiceProvider.GetAllInstances<IMappingConfiguration>();
            Assert.IsNotEmpty(allConfigProviders);
        }

        /// <summary>
        /// List name and column name are url encoded. Try docode them
        /// </summary>
        [Test]
        public void TestMappingDecoding()
        {
            // Get configuration
            var config = new AppConfig();

            // Test xml configuration
            var xmlConfig = config.GetConfiguration();
            Assert.IsNotNull(xmlConfig);
            Assert.IsNotNull(xmlConfig.Mappings);
            var xmlMapping = xmlConfig.Mappings.FirstOrDefault(item => item.ListName == "Obs%c5%82uga+Got%c3%b3wkowa");
            Assert.IsNotNull(xmlMapping);
            Assert.AreEqual(xmlMapping.PropertyMappings[0].ColumnName, "Got%c3%b3wka");

            // Test mapping
            var mapping = config.GetEntityMapping<UrlEncodedTestEntity>();
            Assert.IsNotNull(mapping);
            Assert.AreEqual(mapping.SharepointListName, "Obsługa Gotówkowa");
            var propertyMapping = mapping.PropertiesMapping.FirstOrDefault(item => item.Key == "Cash");
            Assert.IsNotNull(propertyMapping);
            Assert.AreEqual("Gotówka", propertyMapping.Value.ColumnName);
        }
    }
}
